package wang.aweb.childcar.controller.management;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import wang.aweb.childcar.common.ApiRestResponse;
import wang.aweb.childcar.model.pojo.Device;
import wang.aweb.childcar.model.pojo.Member;
import wang.aweb.childcar.model.pojo.WqPage;
import wang.aweb.childcar.model.query.CzOrderParam;
import wang.aweb.childcar.model.query.OrderParam;
import wang.aweb.childcar.model.vo.AgentVO;
import wang.aweb.childcar.model.vo.MemberVO;
import wang.aweb.childcar.service.*;

@RestController
@RequestMapping("/management/")
public class OrderController {
    @Autowired
    private CzOrderService czOrderService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private MemberService memberService;
    @PostMapping("czOrder/list")
    public ApiRestResponse czList(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit,@RequestBody CzOrderParam czOrderParam){
        if(!StringUtils.isEmpty(czOrderParam.getMobile())){
            MemberVO currentMember=memberService.selectByMoblile(czOrderParam.getMobile());
            if(currentMember!=null){
                czOrderParam.setMemberId(currentMember.getId());
            }else{
                czOrderParam.setMemberId(0);
            }
        }
        if(!StringUtils.isEmpty(czOrderParam.getNickname())){
            MemberVO currentMember=memberService.selectByNickname(czOrderParam.getNickname());
            if(currentMember!=null){
                czOrderParam.setMemberId(currentMember.getId());
            }else{
                czOrderParam.setMemberId(0);
            }
        }
        PageInfo pageInfo=czOrderService.getList(page,limit,czOrderParam);
        WqPage wqPage=new WqPage(pageInfo.getTotal(),pageInfo.getList());
        return ApiRestResponse.success(wqPage);
    }
    @PostMapping("orders/list")
    public ApiRestResponse list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, @RequestBody OrderParam orderParam){
        if(!StringUtils.isEmpty(orderParam.getDevicename())){
            Device device=deviceService.findByName(orderParam.getDevicename());
            if(device!=null){
                orderParam.setDeviceId(device.getId());
            }else{
                orderParam.setDeviceId(0);
            }
        }
        if(!StringUtils.isEmpty(orderParam.getMobile())){
            MemberVO currentMember=memberService.selectByMoblile(orderParam.getMobile());
            if(currentMember!=null){
                orderParam.setMemberId(currentMember.getId());
            }else{
                orderParam.setMemberId(0);
            }
        }
        if(!StringUtils.isEmpty(orderParam.getNickname())){
            MemberVO currentMember=memberService.selectByNickname(orderParam.getNickname());
            if(currentMember!=null){
                orderParam.setMemberId(currentMember.getId());
            }else{
                orderParam.setMemberId(0);
            }
        }
        if(!StringUtils.isEmpty(orderParam.getAgentmobile())){
            AgentVO agentVO=agentService.selectByMobile(orderParam.getAgentmobile());
            if(agentVO!=null){
                orderParam.setAgentId(agentVO.getId());
            }else{
                orderParam.setAgentId(0);
            }
        }
        if(!StringUtils.isEmpty(orderParam.getAgentname())){
            AgentVO agentVO=agentService.selectByUsername(orderParam.getAgentname());
            if(agentVO!=null){
                orderParam.setAgentId(agentVO.getId());
            }else{
                orderParam.setAgentId(0);
            }
        }
        if(!StringUtils.isEmpty(orderParam.getEndtime())){
            orderParam.setEndTime(orderParam.getEndtime());
        }
        if(!StringUtils.isEmpty(orderParam.getStarttime())){
            orderParam.setStartTime(orderParam.getStarttime());
        }

        PageInfo pageInfo=orderService.getList(page,limit,orderParam);
        WqPage wqPage=new WqPage(pageInfo.getTotal(),pageInfo.getList());
        return ApiRestResponse.success(wqPage);
    }
}
